package com.huisang.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huisang.springboot.controller.dto.UserDTO;
import com.huisang.springboot.entity.User;
import com.huisang.springboot.service.UserService;
import com.huisang.springboot.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result<UserDTO> login(@RequestBody UserDTO userDTO) {
        return userService.login(userDTO);
    }

    @GetMapping("/{username}")
    public User getUserByName(@PathVariable String username) {
        return userService.getUserByName(username);
    }

    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody User user) {
        //调用插入事务
        Result<Boolean> result = userService.insertUser(user);
        if(result.getCode() == 200) {   //修改插入事务中的message
            result.setMessage("注册成功！");
        } else if(result.getCode() == 600) {
            result.setMessage("用户名重复了，请更换一个");
        } else {
            result.setMessage("注册失败");
        }
        return result;
    }

    @PostMapping
    public Result<Boolean> insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id);
    }

    @PostMapping("/batch")
    public Result<Boolean> deleteBatchUser(@RequestBody List<Integer> ids){
        return userService.deleteBatchUser(ids);
    }

    @PutMapping
    public Result<Boolean> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @GetMapping
    public Result<List<User>> getUserList() {
        return userService.getUserList();
    }

    //分页查询，普通方式
//    @GetMapping("/page")
//    public Result<Map<String, Object>> getUserPageList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
//        return userService.getUserPageList(pageNum, pageSize);
//    }

    //分页查询，mybatis-plus
    @GetMapping("/page")
    public Result<IPage<User>> getUserPageList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(required = false) String username) {
        return userService.getUserPageList(pageNum, pageSize, username);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        userService.export(outputStream);
    }

    @PostMapping("/import")
    public void importEx(MultipartFile file) {
        userService.importEx(file);
    }
}
